using UnityEngine;
using System.Collections;

/// \class  gkSpawningOnGround
/// \brief  Plant the spawned actor on ground
[AddComponentMenu("GK/Engine/Game/Spawn/gkSpawningOnGround")]
public class gkSpawningOnGround : gkSpawningModifier
{
	/// Burried offset
	public float BurriedOffset = 0.5f;
	
    /// \brief  Event called at the and of the actor spawn
    public override void ModifySpawnedActor(GameObject a_rSpawnedActor, int a_iNumActorsInCurrentBatch, int a_iNbActorsInCurrentBatch)
    {
	    PlaceSpawnedActorOnGround(a_rSpawnedActor);
    }

    /// \brief  Place the spawned actor just out of the screen
    void PlaceSpawnedActorOnGround(GameObject a_rSpawnedActor)
    {
		RaycastHit rRayCastHit;
		
		// Ray cast the ground beneath the actor
		if(Physics.Raycast(a_rSpawnedActor.transform.position, Vector3.down, out rRayCastHit,
		                   Mathf.Infinity, 1 << LayerMask.NameToLayer("Ground")))
		{
			a_rSpawnedActor.transform.position = rRayCastHit.point + BurriedOffset * Vector3.down;
		}
		else
		{
			Destroy(a_rSpawnedActor);
		}
    }
}
